<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>开关灯</title>
</head>
<body>
<img src="../../image/pic_bulboff.gif" alt="图片正在加载" width="100" id="myImg">
<br>
<button onclick="changeImg()">开关</button>
</body>
<script>
    function changeImg() {
        var obj = document.getElementById("myImg")
        console.log(obj.src)
        var num = obj.src.search("bulbon")
        console.log(num)


        //if(obj.src == "http://localhost:63342/month02/image/pic_bulboff.gif"){ //关灯
        if (obj.src.search("bulboff") != -1) { //关灯
            obj.src = "../../image/pic_bulbon.gif" //开灯
        } else { //开灯
            obj.src = "../../image/pic_bulboff.gif" //关灯
        }

    }
</script>
</html>